﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace LocalhostMap
{
    /// <summary>
    /// Process 的摘要说明
    /// </summary>
    public class Process : IHttpHandler
    {
        public static string MapPath = "http://www.baidu.com/";

        private bool SetRequestHeader(string Name, out HttpRequestHeader HeaderName)
        {
            switch (Name)
            {
                case "Connection":
                case "Accept":
                case "Host":
                case "Referer":
                    HeaderName = (HttpRequestHeader)0;
                    return false;
                default:
                    Enum.TryParse<HttpRequestHeader>(Name, out HeaderName);
                    return true;
            }
        }
        private bool SetResponseHeader(string Name, out HttpResponseHeader HeaderName)
        {
            switch (Name)
            {
                case "Connection":
                case "Accept":
                case "Host":
                case "Referer":
                    HeaderName = (HttpResponseHeader)0;
                    return false;
                default:
                    Enum.TryParse<HttpResponseHeader>(Name, out HeaderName);
                    return true;
            }
        }

        public void ProcessRequest(HttpContext Context)
        {
            var Request = Context.Request;
            var Response = Context.Response;

            var WebRequest = System.Net.WebRequest.Create(MapPath + Context.Request.RawUrl);

            WebRequest.Method = Request.HttpMethod;

            WebRequest.ContentType = Request.ContentType;
            WebRequest.ContentLength = Request.ContentLength;


            foreach (string Item in Request.Headers)
            {
                HttpRequestHeader Name;

                if (SetRequestHeader(Item,out Name))
                {
                    var Values = Request.Headers.GetValues(Item);

                    foreach (var Value in Values)
                    {
                        WebRequest.Headers.Add(Name, Value);
                    }
                }
            }

            if ("POST".Equals(WebRequest.Method,StringComparison.CurrentCultureIgnoreCase))
            {
                var Bytes = new byte[64];
                int ReadCount;

                using (var OutputStream = WebRequest.GetRequestStream())
                {
                    using (var InputStream = Request.InputStream)
                    {
                        while ((ReadCount = InputStream.Read(Bytes, 0, Bytes.Length)) > 0)
                        {
                            OutputStream.Write(Bytes, 0, ReadCount);
                        }
                    }
                }
            }


            var WebResponse = WebRequest.GetResponse();

            Response.ContentType = WebResponse.ContentType;

            foreach (string Item in WebResponse.Headers)
            {
                HttpResponseHeader Name;

                if (SetResponseHeader(Item, out Name))
                {
                    var Values = WebResponse.Headers.GetValues(Item);

                    foreach (var Value in Values)
                    {
                        Response.Headers.Add(Item, Value);
                    }
                }
            }

            using (var ResponseStream = WebResponse.GetResponseStream())
            {
                using (var TextReader = new StreamReader(ResponseStream, Encoding.UTF8))
                {
                    Context.Response.Write(TextReader.ReadToEnd());
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}